
from rest_framework import serializers
from .models import Notification, NotificationTemplate, WechatSubscription, MessageRecord
from user_app.serializers import UserSerializer
from order_app.serializers import OrderListSerializer


class NotificationSerializer(serializers.ModelSerializer):
    """通知序列化器"""
    
    recipient = UserSerializer(read_only=True)
    notification_type_display = serializers.CharField(source='get_notification_type_display', read_only=True)
    level_display = serializers.CharField(source='get_level_display', read_only=True)
    related_order = OrderListSerializer(read_only=True)
    
    class Meta:
        model = Notification
        fields = [
            'id', 'recipient', 'title', 'content',
            'notification_type', 'notification_type_display',
            'level', 'level_display', 'related_order',
            'is_read', 'read_at', 'created_at'
        ]
        read_only_fields = [
            'id', 'recipient', 'notification_type_display',
            'level_display', 'created_at'
        ]


class NotificationCreateSerializer(serializers.ModelSerializer):
    """通知创建序列化器"""
    
    class Meta:
        model = Notification
        fields = [
            'recipient', 'title', 'content',
            'notification_type', 'level', 'related_order'
        ]


class NotificationTemplateSerializer(serializers.ModelSerializer):
    """通知模板序列化器"""
    
    notification_type_display = serializers.CharField(source='get_notification_type_display', read_only=True)
    level_display = serializers.CharField(source='get_level_display', read_only=True)
    
    class Meta:
        model = NotificationTemplate
        fields = [
            'id', 'code', 'title_template', 'content_template',
            'notification_type', 'notification_type_display',
            'level', 'level_display', 'wechat_template_id',
            'is_active', 'remark', 'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'notification_type_display', 'level_display',
            'created_at', 'updated_at'
        ]


class WechatSubscriptionSerializer(serializers.ModelSerializer):
    """微信订阅消息序列化器"""
    
    user = UserSerializer(read_only=True)
    
    class Meta:
        model = WechatSubscription
        fields = [
            'id', 'user', 'template_id', 'subscription_id',
            'is_active', 'expires_at', 'created_at', 'updated_at'
        ]
        read_only_fields = [
            'id', 'user', 'created_at', 'updated_at'
        ]


class WechatSubscriptionCreateSerializer(serializers.ModelSerializer):
    """微信订阅消息创建序列化器"""
    
    class Meta:
        model = WechatSubscription
        fields = ['template_id', 'subscription_id', 'expires_at']


class MessageRecordSerializer(serializers.ModelSerializer):
    """消息发送记录序列化器"""
    
    notification = NotificationSerializer(read_only=True)
    message_type_display = serializers.CharField(source='get_message_type_display', read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    
    class Meta:
        model = MessageRecord
        fields = [
            'id', 'notification', 'message_type', 'message_type_display',
            'recipient_id', 'title', 'content', 'template_data',
            'status', 'status_display', 'error_message', 'external_id',
            'created_at', 'sent_at', 'delivered_at'
        ]
        read_only_fields = [
            'id', 'message_type_display', 'status_display',
            'created_at', 'sent_at', 'delivered_at'
        ]


class NotificationListSerializer(serializers.ModelSerializer):
    """通知列表序列化器"""
    
    notification_type_display = serializers.CharField(source='get_notification_type_display', read_only=True)
    level_display = serializers.CharField(source='get_level_display', read_only=True)
    
    class Meta:
        model = Notification
        fields = [
            'id', 'title', 'notification_type', 'notification_type_display',
            'level', 'level_display', 'is_read', 'created_at'
        ]
        read_only_fields = [
            'id', 'notification_type_display', 'level_display', 'created_at'
        ]